package com.qcxy.gzy.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;

import com.qcxy.gzy.activity.base.HiddenActivity;
import com.qcxy.gzy.task.GetScanQRImageTask;
import com.qcxy.gzy.task.GetUserNameTask;
import com.squareup.picasso.Picasso;

public class ScanActivity extends HiddenActivity implements OnClickListener {

	private ImageView mIvScan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scan);

		mIvScan = (ImageView) findViewById(R.id.iv_scan);
		findViewById(R.id.iv_back).setOnClickListener(this);
	}

	// Activity创建或者从被覆盖、后台重新回到前台时被调用
	@Override
	protected void onResume() {
		super.onResume();
		// 获取微信JPG图片
		new GetScanQRImageTask(this).execute();
		// 延迟两秒后执行轮查
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			public void run() {
				new GetUserNameTask(ScanActivity.this).execute();
			}
		}, 2000);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.iv_back: {
			ScanActivity.this.finish();
			break;
		}

		default:
			break;
		}
	}

	public void getQRImage(JSONObject result) {
		try {
			final JSONObject jsonObject = result.getJSONObject("data");
			if (jsonObject == null) {
				return;
			}
			String imageUrl = jsonObject.optString("imagePath");
			// 获取QRCode
			Picasso.with(ScanActivity.this).load(imageUrl).into(mIvScan);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
}
